//
//  @ Project : Galaxy Star Trading System
//  @ File Name : BarFactory.h
//  @ Date : 2014/9/5
//  @ Author :
//
//


#if !defined(_CBARFACTORY_H_H)
#define _CBARFACTORY_H_H

namespace GSTrader
{
	namespace QuoteService
	{
		class CBarFactory;
		class CRuntimeDateTime;
		class QUOTE_SERVICE_API CBarFactoryItem
		{
		public:
			CBarFactoryItem();
			CBarFactoryItem(int nInstrumentID, short nBarType, short nBarCapacity);
			virtual ~CBarFactoryItem();

		protected:
			void EmitBarData();
			virtual void OnNewQuote(CDepthMarketData* pMarketData);
			virtual void OnTimer(CTimePoint& t);
			bool AddTimer(CTimePoint& t);

		protected:
			int m_nInstrumentID;

			short m_nBarType;
			short m_nBarCapacity;

			CBarData* m_pBarData;
			CBarFactory* m_pBarFactory;

			friend class CBarFactory;
		};

		class QUOTE_SERVICE_API CTimeBarFactoryItem : public CBarFactoryItem
		{
		public:
			CTimeBarFactoryItem();
			CTimeBarFactoryItem(int nInstrumentID, short nBarType, short nBarCapacity);
			virtual ~CTimeBarFactoryItem();

			virtual void OnNewQuote(CDepthMarketData* pMarketData);
			virtual void OnTimer(CTimePoint& t);
		};

		class QUOTE_SERVICE_API CTickBarFactoryItem : public CBarFactoryItem
		{
		public:
			CTickBarFactoryItem();
			CTickBarFactoryItem(int nInstrumentID, short nBarType, short nBarCapacity);
			virtual ~CTickBarFactoryItem();

			virtual void OnNewQuote(CDepthMarketData* pMarketData);
		};

		class QUOTE_SERVICE_API CVolumeBarFactoryItem : public CBarFactoryItem
		{
		public:
			CVolumeBarFactoryItem();
			CVolumeBarFactoryItem(int nInstrumentID, short nBarType, short nBarCapacity);
			virtual ~CVolumeBarFactoryItem();

			virtual void OnNewQuote(CDepthMarketData* pMarketData);
		};

		class QUOTE_SERVICE_API CRangeBarFactoryItem : public CBarFactoryItem
		{
		public:
			CRangeBarFactoryItem();
			CRangeBarFactoryItem(int nInstrumentID, short nBarType, short nBarCapacity);
			virtual ~CRangeBarFactoryItem();

			virtual void OnNewQuote(CDepthMarketData* pMarketData);
		};

		typedef list<CBarFactoryItem*> ListBarFactorItem;
		typedef function<void(CMsg*)> MsgDispatchFunc;
		typedef function<void(CTimePoint, CMsg*)> TimerFunc;
		typedef map<int, ListBarFactorItem> MapBarByCapacity;
		typedef map<time_t, MapBarByCapacity> MapTimerByTime;

		class QUOTE_SERVICE_API CBarFactory
		{
		public:
			CBarFactory();
			virtual ~CBarFactory();

			bool AddNewItem(int nInstrumentID, short nBarType, short nBarCapacity);
			void OnMarketData(CDepthMarketData* pMarketData);

			CBarFactoryItem* GetBarFactoryItem(int nInstrumentId, short nBarType, short nBarCapacity);
			ListBarFactorItem* GetBarFactoryItemList(int nInstrumentId);
			void SetMsgDespatchFunc(MsgDispatchFunc& pFunc);

			virtual void OnTimer(CTimePoint& t, CMsg* pMsg);
			bool AddTimer(CBarFactoryItem* pItem, CTimePoint& t);

		public:
			CRuntimeDateTime* m_pRunTimeDateTime;

		private:
			bool AddNewItem(CBarFactoryItem* pItem);
			void RemoveAllItem();

			map<int, ListBarFactorItem> m_mapItem;
			MsgDispatchFunc m_pFunc;
			MapTimerByTime  m_mapTimer;
			friend class CBarFactoryItem;
		};
	}
}

#endif // _CBARFACTORY_H_H